home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12058 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: news1.intercall.com!usenet
  2. From: engevar@intercall.com (Steven Ovits)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Dynamic 2-D arrays
  5. Date: Fri, 29 Mar 1996 02:56:55 GMT
  6. Organization: Intercall Inc.
  7. Message-ID: <4jf9t1$98@news1.intercall.com>
  8. References: <40.69871.1612@channel1.com>
  9. NNTP-Posting-Host: ts2-112.intercall.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. dspse.bedford@channel1.com (Dspse Bedford) wrote:
  13.  
  14. >I want to define a 2-D array through a pointer-to-pointer structure member
  15. >as shown below:
  16.  
  17. >typedef struct
  18. >{ float **matrix;}Obj;
  19. >main()
  20. >{Obj *m;
  21. > int x=5;y=10;
  22. > m=(Obj *)malloc(sizeof(Obj));
  23. > *(m->matrix)=(float *)malloc(x*y*sizeof(float *));
  24. >  ...
  25. > ...
  26. > return;}
  27.  
  28. >if I index in the following way: m->matrix[i][j], how does it increment the 
  29. >pointer to determine the memory location?  If I have 
  30. >m->matrix[1][2] and m->matrix[2][1] what is the difference?
  31.  
  32. First, I hope you have some real memory at that location, you
  33. allocated the pointers but not the floats.  You also have to point
  34. the float* to the memory allocated for the floats.
  35.  
  36. The compiler treats a[][j] as if it were a type definition, and
  37. calculates a[i][j] as:   *(a + i * sizeof(a[][j]) + j).
  38. You should notice that the compiler must know the number of
  39. elements in a row--in all rows except the first. That takes some
  40. getting used to, but consider how you would get to a[1][0][0][0]
  41. in a[][x][y][z] without knowing x, y, and z.
  42.  
  43. Now you can try calculating m[1][2] and m[2][1].
  44.  
  45. More formally, given float matrix[i][j], 
  46. matrix[p][q] is evaluated as *( * (matrix+p) + q)
  47. but this hides the details.
  48.  
  49. To answer your question, write a macro.
  50. #define idx2d(Array, i, j, dim1, type) \
  51. (type) ( (Array) + (((i) * (dim1)) + (j)) * sizeof(type) )
  52.  
  53. >In the past, I would use something like whats below, but the difference is 
  54. >I do now know ahead of time the Y_dimension or the X_dimension.
  55.  
  56. >#define Y_DIM 10
  57. >#define X_DIM  5
  58. >typedef struct
  59. >{float *matrix[Y_DIM];}Obj;
  60. >main()
  61. >{
  62. >  Obj *m;
  63. >  m=(Obj *)malloc(sizeof(Obj));
  64. >  for (i=0;i<Y_DIM;++i)
  65. >  { m->matrix[i]=(float *)malloc(X_DIM*sizeof(float));}
  66. > ..
  67. > return;}
  68.  
  69. >The above ofcourse indexes fine since I create an array of pointers of the 
  70. >proper Y-dimension in my structure definition.
  71.  
  72. >Any help would be appreciated.
  73.  
  74. >Thank you,
  75. >Anastasios Maurudis
  76. >anastasios@dspse.com
  77. >DSP Software Engineering, Inc.
  78. >175 Middlesex Turnpike
  79. >Bedford, MA 01730
  80. >(617)275-3733
  81. >(617)275-4323 Fax
  82. >   
  83.  
  84. >---
  85. > * WR 1.32 # 331 * Blue Wave - what Smurfs do at a football game..
  86.  
  87.  
  88.